const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const Demo = require('../model/project_and_wz');
const {
    json
} = require('../util/entry');
//分页查找功能
const selectAll = async function (index, number, limits, type) {
    try {
        const ins = await Demo.findAndCountAll({
            offset: (index - 1) * number, // 跳过几个
            limit: number, //取几个
            order: [
                ['id', 'DESC']
            ], //倒序输出
            where: {
                // demoClassification: limits
                classification: {
                    [Op.like]: '%' + limits + '%',
                },
                type: {
                    [Op.like]: '%' + type + '%',
                }
            }
        });
        const data = json(ins);
        return data;
    } catch (err) {
        return json(err);
    }
}

//添加demo
const add_demo = async function (title, synopsis, up_time, link, cover, classification) {
    try {
        const data = await Demo.create({
            title,
            synopsis,
            up_time,
            content: null,
            classification,
            author: "Lv's Code",
            reading: 0,
            type: '2,0',
            message: null,
            cover,
            link
        });
        return json(data);
    } catch (err) {
        return json(err);
    }
}

//添加text
const add_txt = async function (title, synopsis, up_time, content, classification) {
    try {
        const data = await Demo.create({
            title,
            synopsis,
            up_time,
            content,
            classification,
            author: "Lv's Code",
            reading: 0,
            type: '2,1',
            message: 0,
            cover: null,
            link: null
        });
        return json(data);
    } catch (err) {
        return json(err);
    }
}

//浏览量增加功能
const updateLook = async function (id) {
    try {
        const data = await Demo.findAll({
            where: {
                id
            }
        })
        let number = (+json(data)[0].reading) + 1;
        console.log(number, data);
        const ins = await Demo.update({
            reading: number
        }, {
            where: {
                id
            }
        });
        return json(ins);
    } catch (err) {
        return json(err);
    }
}

//浏览指定ID信息
const getIdInfo = async function (id) {
    try {
        const data = await Demo.findAll({
            where: {
                id
            }
        })
        return json(data);

    } catch (err) {
        return json(err);
    }
}

//hot文章
const get_hot_a = async function () {
    try {
        const ins = await Demo.findAndCountAll({
            offset: 0, // 跳过几个
            limit: 10, //取几个
            order: [
                ['reading', 'DESC']
            ], //倒序输出
            where: {
                type: {
                    [Op.like]: '%' + "2,1" + '%',
                }
            }
        });
        const data_temp = json(ins);
        const data = [];
        (data_temp.rows).forEach((ele, i) => {
            if (ele.id != -1) {
                data.push({
                    id: ele.id,
                    title: ele.title
                })
            }

        });
        return data;
    } catch (err) {
        return json(err);
    }
}

//hot文章
const get_hot_p = async function () {
    try {
        const ins = await Demo.findAndCountAll({
            offset: 0, // 跳过几个
            limit: 10, //取几个
            order: [
                ['reading', 'DESC']
            ], //倒序输出
            where: {
                type: {
                    [Op.like]: '%' + "2,0" + '%',
                }
            }
        });
        const data_temp = json(ins);
        const data = [];
        (data_temp.rows).forEach((ele, i) => {
            if (ele.id != -1) {
                data.push({
                    id: ele.id,
                    title: ele.title,
                    href: ele.link
                })
            }

        });
        return data;
    } catch (err) {
        return json(err);
    }
}



module.exports = {
    selectAll,
    updateLook,
    getIdInfo,
    get_hot_p,
    get_hot_a,
    add_txt,
    add_demo
}